
<?php include_once(dirname(__FILE__) . '/../general/head.php'); ?>

<!-- header -->
<?php include_once(dirname(__FILE__) . '/../general/header.php'); ?>

<?php include_once('left.php'); ?>


<div id="page-wrapper">

    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">Ngân hàng câu hỏi</h1>
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Danh sách
                </div>
                <!-- /.panel-heading -->
                <div class="panel-body">

                    <div class="col-lg-12">
                        Chủ đề 
                        <select class="form-control display-inline" name="filter-category" style="width: 150px;">
                            <option value="all">Tất cả</option>
                            <?php foreach ($category as $eachCategory) { ?>
                                <option value="<?php echo $eachCategory->cid; ?>" <?php echo ($filterCategory == $eachCategory->cid) ? 'selected="selected"' : ''; ?>><?php echo $eachCategory->category_name; ?></option>
                            <?php } ?>
                        </select>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        Mức độ
                        <select class="form-control display-inline" name="filter-difficult" style="width: 100px;">
                            <option value="all">Tất cả</option>
                            <?php
                            foreach ($diffDic as $key => $value) {
                                ?>
                                <option value="<?php echo $key; ?>" <?php echo ($filterDifficult == $key) ? 'selected="selected"' : ''; ?>><?php echo $value; ?></option>
                            <?php } ?>
                        </select>
                        <br><br>
                    </div>
                    <div class="col-lg-12">
                        <div class="table-responsive">
                            <table class="table table-striped table-bordered table-hover sb-table">
                                <thead>
                                    <tr>
                                        <!--<th width="30px">Id</th>-->
                                        <th width="55px">&nbsp;</th>
                                        <th width="120px">Chủ đề</th>
                                        <th width="90px">Mức độ</th>
                                        <th>Câu hỏi</th>
                                        
                                        <th width="60px"><font color="green">Đúng</font></th>
                                        <th width="60px"><font color="red">Sai</font></th>
                                        <th width="80px">Tổng số</th>
                                        <th width="60px"><font color="green">% đúng</font></th>
                                        <th width="60px"><font color="red">% sai</font></th>
                                        <th width="80px">&nbsp;</th>
                                        
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    if ($result == false) {
                                        ?>
                                        <tr>
                                            <td colspan="5">
                                                No record found!
                                            </td>
                                        </tr>
                                        <?php
                                    } else {
                                        foreach ($result as $row) {
                                            ?>
                                            <tr>
                                                <!--<td><?php echo $row['qid']; ?></td>-->
                                                <td>
                                                    <a href="javascript: if(confirm('Bạn có chắc là muốn xóa không? question?')){ window.location='<?php echo site_url(get_folder($loggedUser['title']) . '/questions/remove_question/' . $row['qid']); ?>'; }">
                                                        <img src="<?php echo base_url(); ?>images/cross.png" title="Remove">
                                                    </a>  
                                                    <a href="<?php echo site_url(get_folder($loggedUser['title']) . '/questions/edit_question/' . $row['qid']); ?>">
                                                        <img src="<?php echo base_url(); ?>images/edit.png" title="Edit">
                                                    </a>
                                                </td>
                                                <td><?php echo $cateDic[$row['cid']]; ?></td>
                                                <td><?php echo $diffDic[$row['did']]; ?></td>
                                                <td><?php echo $row['question']; ?></td>
                                                
                                                <td><font color="green"><?php echo $row['rights']; ?></font></td>
                                                <td><font color="red"><?php echo $row['wrongs']; ?></font></td>
                                                <td><?php echo $row['rights'] + $row['wrongs']; ?></td>
                                                <td><font color="green"><?php echo round($row['rightsRatio'])?></font>
                                                </td>
                                                <td><font color="red"><?php echo round($row['wrongsRatio']); ?></font>
                                                </td>
                                                <td>
                                                    <button class="btn btn-link btn-xs" data-toggle="modal" data-target="#myModal" name="stat" id="stat-<?php echo $row['qid'] ?>">
                                                        Chi tiết...
                                                    </button>
                                                </td>
                                                
                                            </tr>

                                            <?php
                                        }
                                    }
                                    ?>

                                </tbody>
                            </table>
                            <br>
                            <a href="<?php echo site_url(get_folder($loggedUser['title']) . '/questions/add_new'); ?>"> 
                                <img src="<?php echo base_url(); ?>images/plus.png" title="Thêm câu hỏi mới"> Thêm câu hỏi mới
                            </a>
                            <br><br>
                            <a href="<?php echo site_url(get_folder($loggedUser['title']) . '/questions/export'); ?>" class="btn btn-primary">Xuất ra file csv</a>

                        </div>
                    </div>
                </div>
                <!-- /.panel-body -->
            </div>
            <!-- /.panel -->
            
            <!--Modal-->
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h4 class="modal-title" id="myModalLabel">Chi tiết đúng sai</h4>
                        </div>
                        <div class="modal-body">
                            <table class="table table-responsive table-bordered" id="stat-options">
                                <thead>
                                    <th>Lựa chọn</th>
                                    <th width="140px">Số người chọn</th>
                                </thead>
                                <tbody>
                                    
                                </tbody>
                            </table>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Đóng</button>
                            <!--<button type="button" class="btn btn-primary">Save changes</button>-->
                        </div>
                    </div>
                    <!-- /.modal-content -->
                </div>
                <!-- /.modal-dialog -->
            </div>
            
            <div class="panel panel-default">
                <div class="panel-heading">
                    Upload câu hỏi
                </div>
                <div class="panel-body">
                    <div class="table-responsive">
                        <table id="detail" class="table table-bordered table-hover">
                            <tr>
                                <td>	<?php echo form_open(get_folder($loggedUser['title']) . '/questions/import', array('enctype' => 'multipart/form-data')); ?>
                                    <br>
                                    <select name="cid">
                                        <option value="0">Select Category</option>
                                        <?php foreach ($category as $value) { ?>
                                            <option value="<?php echo $value->cid; ?>">
                                                <?php echo $value->category_name; ?>
                                            </option>
                                        <?php } ?>
                                    </select>

                                    <select name="did">
                                        <option value="0">Select level</option>
                                        <?php foreach ($difficult_level as $value) { ?>
                                            <option value="<?php echo $value->did; ?>">
                                                <?php echo $value->level_name; ?>
                                            </option>
                                        <?php } ?>
                                    </select> 
                                    Upload file excel (dạng .xls)
                                    <br><br>
                                    <input type="hidden" name="size" value="3500000">
                                    <input type="file" name="xlsfile" >
                                    <br>
                                    <input type="submit" value="Import"></form>
                                    <br>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <!-- /.col-lg-12 -->
    </div>


</div>
<!-- /#page-wrapper -->



<script>
    function search()
    {
        var cid = "";
        var did = "";

        $("select[name=filter-category] option:selected").each(function() {
            cid = $(this).val();
        });
        $("select[name=filter-difficult] option:selected").each(function() {
            did = $(this).val();
        });
        window.location = '<?php echo site_url(get_folder($loggedUser['title']) . '/questions/index') ?>' + '/' + cid + '/' + did;
    }

    $(document).ready(function() {
//        var oTable = $('#att-detail-results').dataTable(
//                {
//                    "scrollX": true,
//                    "sScrollX": "120%",
//                    "columnDefs": [
//                        {
//                            "targets": [0],
//                            "visible": false,
//                            "searchable": false,
//                            "orderData": [0, 1]
//                        },
//                        {
//                            "targets": [1],
//                            "visible": false,
//                            "searchable": false,
//                            "orderData": [0, 1]
//                        },
//                        {
//                            "targets": [2],
//                            "visible": false,
//                            "searchable": false,
//                            "orderData": [0, 1]
//                        }
//                    ]
//                });

        $('select[name=filter-category]').change(function() {
            search();
        });

        $('select[name=filter-difficult]').change(function() {
            search();
        });
        
        
        $("button[name=stat]").click(function() {
            var qid = $(this).attr('id');
                $.ajax({
                    url: '<?php echo site_url(get_folder($loggedUser['title']) . '/questions/stat_option/'); ?>',
                    type: 'post',
                    data: {"questionId": qid},
                    beforeSend : function (){
                        $('#stat-options tbody').html('<tr><td colspan="2">Chờ chút, đang tính toán <img src="<?php echo base_url(); ?>images/loading1.gif" alt="Đang tính toán"/></td></tr>');
                    },
                    success: function(data) {
                        
                        if (data.success == true)
                        {
                            $('#stat-options tbody').html('');
                            for (var i = 0; i < data.result.length; i++) {
                                var htmlString;
                                if(data.result[i][1] == 1)
                                {
                                    htmlString = '<tr style="color: green; font-weight:bold;"><td>'+ data.result[i][0]
                                    +'</td><td style="text-align: right;">'+data.result[i][2]
                                    +'</td></tr>';
                                }
                                else
                                {
                                    htmlString = '<tr><td>'+ data.result[i][0]
                                    +'</td><td style="text-align: right;">'+data.result[i][2]
                                    +'</td></tr>';                                    
                                }
                                
                                $('#stat-options tbody').append(htmlString);
                            }
                        }
                        else
                        {
                            alert(data.message);
                        }
                    },
                    error: function(xhr, strErr) {
                        //alert('Lỗi: ' + "\n" + xhr + "\n" + strErr);
                        
                        htmlString = '<tr><td>'+ xhr
                                    +'</td><td>'+ strErr
                                    +'</td></tr>';
                            
                        $('#stat-options tbody').append(htmlString);
                    }
                });
        });
    });

</script>

<?php include_once(dirname(__FILE__) . '/../general/footer.php'); ?>